/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package IpChat_For_Testing;

/**
 *
 * @author lubinchen
 */



import java.net.InetAddress;
import java.util.*;




public class IpChat_Constructor {

   public static int port=4001;
   public ArrayList<Hashtable> received_ht_al=new ArrayList<Hashtable>(0);
   public ArrayList<Boolean> clientconnected=null;
   private InetAddress tempIpAdd=null;
   private GUI gui=null;
   public static Boolean have_new_text=false;
   public IpChat_Main mymain=null;
   public static String received_new_text;
   public ChatServer server=null;
   public static ChatClient client=null;
   public static ChatClient clients[] = new ChatClient[20];
   public static Integer clientCount=0;
   public String MyOwnIP;
   private InetAddress ownIP;





   public IpChat_Constructor(ArrayList<Hashtable> ht, IpChat_Main newmain){


       System.out.println("New object of callthis created");

        // initialize the attributes
            update_ht(ht);

        // after all arrayList is done, start new server, assuming port=4000
            mymain= newmain;

            server=new ChatServer(port, this);


         try{
	      ownIP=InetAddress.getLocalHost();
              MyOwnIP=ownIP.getHostAddress();
	    }catch (Exception e){
	      System.out.println("Exception caught "+e.getMessage());
	    }



        for (int i=0; i< received_ht_al.size();i++)
            if( received_ht_al.get(i).get("ip")==MyOwnIP )
            {
              InetAddress Inet= getInet( (String)  received_ht_al.get(i).get("ip") );
              Integer team=(Integer)received_ht_al.get(i).get("team");
              clients[clientCount] = new ChatClient( Inet, port, (String)received_ht_al.get(i).get("username"), this,team );
              break;
            }




            checkunconnected();
   }


   
   public void update_ht(ArrayList<Hashtable> New_ht){
       int hti, i;
       // create new client info
       for (hti = 0; hti < New_ht.size(); hti++)
       {
               Boolean existAlready=false;
      
               for(i=0; i<received_ht_al.size() ;i++ )
                   if (received_ht_al.get(i).get("ip")==null ? New_ht.get(hti).get("ip")==null: received_ht_al.get(i).get("ip").equals(New_ht.get(hti).get("ip")))
                   {
                        existAlready = true;
                        break;
                   }
         
               if(!existAlready)
               {
                    received_ht_al.add(New_ht.get(hti));
                    Boolean connected=false;
                    Hashtable temh=received_ht_al.get(received_ht_al.size()-1);
                    temh.put("connected", connected);
               }
       }

       
      // update team info if neccessary
       for (hti = 0; hti < New_ht.size(); hti++)
       {
               for(i=0; i<received_ht_al.size() ;i++ )
                 if (received_ht_al.get(i).get("ip").equals(New_ht.get(hti).get("ip")))
                    if (received_ht_al.get(i).get("team")!=New_ht.get(hti).get("team") )
                    {
                        received_ht_al.get(i).remove("team");
                        Integer newT= (Integer)New_ht.get(hti).get("team");
                        received_ht_al.get(i).put("team", newT);
                    }                      
       }
       
       System.out.println();
       System.out.println("HT Update !Pelase note that the updated hashtable received_ht_al has.. ");
          for (int p=0;p<received_ht_al.size();p++)
            System.out.println("Line "+p+": "+received_ht_al.get(p).toString());
       System.out.println();
   
   }




   private void ConnectClients(){

        for (int i=0; i< received_ht_al.size();i++)
            if( (Boolean)received_ht_al.get(i).get("connected")==false )
            {
              InetAddress Inet= getInet( (String)  received_ht_al.get(i).get("ip") );
              Integer team=(Integer)received_ht_al.get(i).get("team");
              clients[clientCount] = new ChatClient( Inet, port, (String)received_ht_al.get(i).get("username"), this,team );
                
            }
        
        checkunconnected();
   }

   public void connection_successful(String clientsIP){


       for(int i=0; i<received_ht_al.size(); i++)
           if(received_ht_al.get(i).get("ip")==null? clientsIP==null : received_ht_al.get(i).get("ip").equals(clientsIP))
           {
               received_ht_al.get(i).remove("connected");
               Boolean connected=true;
               received_ht_al.get(i).put("connected", connected);
               //System.out.println("received_ht_al.get(i).get(username)= "+received_ht_al.get(i).get("username")+".get(connected)= "+ received_ht_al.get(i).get("connected"));

           }



       System.out.println();
       System.out.println("HT Update ! Pelase note that the updated hashtable received_ht_al has.. ");
          for (int p=0;p<received_ht_al.size();p++)
            System.out.println(received_ht_al.get(p).toString());
       System.out.println();


   }



   public void checkunconnected(){

       for (int i=0; i< received_ht_al.size();i++)
            if((Boolean)received_ht_al.get(i).get("connected")==false)
                 ConnectClients();
   }


    private InetAddress getInet(String DestinationIP)
    {

        try{
            tempIpAdd= InetAddress.getByName(DestinationIP);

            }catch (Exception e){}

        return tempIpAdd;
    }


}

